package paginator.classes
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.TitleWindow;
	import mx.controls.Menu;
	
	import paginator.buttons.FirstBtn;
	import paginator.buttons.NextBtn;
	import paginator.buttons.PreviousBtn;
	import paginator.helpers.MenuButtonsCreator;
	import paginator.paginateEvents.PagesValueChange;
	
	/**
	* This class support all buttons
	 * Previous, Jump back and Jump first page
	 *  
	 * @author Hellix
	 */	
	public class PreviousButtonsClass extends ButtonsClass implements IObserver
	{
		//---------------------------------------------------------------
		// <------ PRIVATE PROPERTIES ------>
		//---------------------------------------------------------------
		//{
		/**
		 *Button object of previous button 
		 */		
		private var _previousBtn:PreviousBtn;
		/**
		 *Button object of jump to first page
		 */		
		private var _firstBtn:FirstBtn;
		
		//}
		//---------------------------------------------------------------
		// <------ PUBLIC METHODS ------>
		//---------------------------------------------------------------
		//{
		
		/**
		 * Ctor 
		 * @param PreviousBtn previousBtn NextBtn object
		 * @param FirstBtn firstBtn LastBtn object 
		 */	
		public function PreviousButtonsClass(previousBtn:PreviousBtn, firstBtn:FirstBtn)
		{
			super();
			
			_previousBtn = previousBtn;
			_firstBtn = firstBtn;
			
			_previousBtn.heightPreviousBtn = super._arrowBtnHeight;
			_firstBtn.height = super._arrowBtnHeight;
		}
		
		/**
		 * This function notify paginator manager 
		 * */
		public function notifyObserver():void
		{
			_observer.updateElements();
		}
		
		/**
		 * This function sets paginator manager and register required events
		 *  
		 * @param PaginatorManager mainManager Object PaginatorManager
		 */	
		override public function setObserver(mainManager:PaginatorManager):void
		{
			if (_previousBtn.isMenuRefresh)
			{
				super._isMenuRefresh = true;
				_previousBtn.isMenuRefresh = false;
			}
			
			super.setObserver(mainManager);
			
			super._observer.paginator.addEventListener(PreviousBtn.CLICK_SKIPPREVIOUS, onContextMenuButton);
			super._observer.paginator.ID_PreviousButtons.ID_Previous.addEventListener(MouseEvent.CLICK, onClickPrevious);
			super._observer.addEventListener(PagesValueChange.NUMBEROFPAGES_CHANGE, onNumOfPagesChange);
			super._observer.paginator.ID_Last.addEventListener(MouseEvent.CLICK, onClickLastBtn);
		}
		
		//}
		//---------------------------------------------------------------
		// <------ PROTECTED METHODS ------>
		//---------------------------------------------------------------
		//{
		
		override protected function onContextMenuButton(event:Event):void
		{
			super.onContextMenuButton(event);
			
			if (super._menuButton)
			{
				super._xPositionMenu = super._observer.paginator.x + _previousBtn.x;
				super._yPositionMenu = super._observer.paginator.y + _previousBtn.ID_SkipPrevious.height;
				super._menuButton.show(super._xPositionMenu, super._yPositionMenu);
			}
		}
		
		//}
		//---------------------------------------------------------------
		// <------ PRIVATE METHODS ------>
		//---------------------------------------------------------------
		//{
		
		private function onClickPrevious(event:MouseEvent):void
		{
			if (0 < _observer.selectedPage)
			{
				_observer.selectedPage -= 1;
			}
		}
		
		private function onNumOfPagesChange(event:PagesValueChange):void
		{
			if (event.numberOfPages < 50)
			{
				_previousBtn.visibleSkipPrevButton = false;
				super._menuButton = null;
			}
		}
		
		private function onClickLastBtn(event:MouseEvent):void
		{
			super.jumpFirstOrLastPage(1);
		}
		
		//}
	}
}